<?php
    session_start();
    error_reporting(0);
    define('__ROOT__', dirname(dirname(__FILE__)));
    require_once(__ROOT__.'\config\config.php'); 
    require_once(__ROOT__.'\model\db.php');
    require_once(__ROOT__.'\model\user.php');
    $user = new User();
    if(isset($_POST['per_x']))
    {
        if($_GET['per'] == 3)
            $users = $user->update($_GET['id'],"1");
        else
            $users = $user->update($_GET['id'],"3");
        echo("<script>location.href = '".HOST."view/view_listaccount.php?action=succeed';</script>");
    }
    if(isset($_POST['block_x']))
    {
        if($_GET['block'] == 0)
            $users = $user->block($_GET['id'],"1");
        else
            $users = $user->block($_GET['id'],"0");        
        echo("<script>location.href = '".HOST."view/view_listaccount.php?action=succeed';</script>");
    }
    if(isset($_POST['delete_x']))
    {
        $users = $user->delete($_GET['id']);
        echo("<script>location.href = '".HOST."view/view_listaccount.php?action=succeed';</script>");
    }
    if(isset($_POST['submit']))
    {
        $oldpassword = @$_POST['opass'];
        $password = @$_POST['sipass'];
        $repassword = @$_POST['resipass'];
        $users = $user->select($_SESSION['email']);
        if($oldpassword != $users[0]['pass'])
        {
            echo $users[0]['pass'];
            echo("<script>location.href = '".HOST."view/view_errorlogin.php?error=2&action=1';</script>");
        }
        else
        {
            if(strlen($password) > 4 && strlen($password) < 25 && strlen($repassword) > 4 && strlen($repassword) < 25)
            {
                if($password == $repassword)
                {
                    $users = $user->updatepass($_SESSION['email'], $password);      
                    echo("<script>location.href = '".HOST."view/view_errorlogin.php?error=6&action=1';</script>");
                }
                else
                    echo("<script>location.href = '".HOST."view/view_errorlogin.php?error=4&action=1';</script>");
            }
            else
            {
                echo("<script>location.href = '".HOST."view/view_errorlogin.php?error=3&action=1';</script>");
            }
        }
    }
?>